﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using Telerik.Web.UI;
using ud_gSDMS.Code_Service;
using System.Web.UI;

namespace ud_gSDMS.contract
{
    public partial class Contract_HostDomain_Report : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private SiteUser _siteUser;

        void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //if (!UserCanEditPage() || !UserCanViewPage())
                //{
                //    WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
                //}
                _siteUser = SiteUtils.GetCurrentSiteUser();


                chkExpire.Attributes.Add("onclick", "fchkExpire_CheckedChanged(this)");
                chkRenew.Attributes.Add("onclick", "fchkCanRenew_CheckedChanged(this)");
                if (!Page.IsPostBack)
                {
                    PopulateLabels();
                    PopulateControls();
                }

            }
            catch
            {
            }
        }
        private void PopulateLabels()
        {

        }
        private void PopulateControls()
        {
            datContract_Date_from.SelectedDate = DateTime.Now.AddYears(-2);
            datContract_Date_to.SelectedDate = DateTime.Now;
            

            LoadControl();
            LoadGrid();
        }
        private void LoadGrid()
        {
            var data = gSDMS_BL.ud_gSDMS_HOSTING_CONTRACT_Report_Search(datContract_Date_from.SelectedDate == null ? null : datContract_Date_from.SelectedDate.Value.ToShortDateString(),
                                                                    datContract_Date_to.SelectedDate == null ? null : datContract_Date_to.SelectedDate.Value.ToShortDateString(),

                                                                cboContract_Code.Text, cboCompany_Name.Text,
                                                                chkHosting.Checked, chkDomain.Checked , chkExpire.Checked, 
                                                                 !chkDelete.Checked , chkRenew.Checked );
            if (ViewState["SortExpression"] != null)
            {
                var dv = data.DefaultView;
                dv.Sort = ViewState["SortExpression"].ToString();
                data = dv.Table;
            }
            //row["World_Domain"] = r["World_Domain"];
            //row["VietNam_Domain"] = r["VietNam_Domain"];
            //var ex = string.Format("PackageName not like '%{0}%' and World_Domain not like '%{1}%' and VietNam_Domain", "");
            //data.Select(ex);

            

            grdHostngContractDomain.DataSource = data;
            grdHostngContractDomain.DataBind();
        }
        private void LoadControl()
        {
            var temp = gSDMS_BL.ud_gSDMS_HOSTING_CONTRACT_Lst();
            cboContract_Code.DataValueField = "Hosting_Contract_ID";
            cboContract_Code.DataTextField = "Contract_Code";
            cboContract_Code.DataSource = temp;
            cboContract_Code.DataBind();
            cboContract_Code.Items.Insert(0 , new RadComboBoxItem("" , ""));
            cboContract_Code.SelectedValue = "";

            cboCompany_Name.DataValueField = "Hosting_Contract_ID";
            cboCompany_Name.DataTextField = "Company_Name";
            cboCompany_Name.DataSource = temp;
            cboCompany_Name.DataBind();
            cboCompany_Name.Items.Insert(0, new RadComboBoxItem("", ""));
            cboCompany_Name.SelectedValue = "";

        }

        private void grdHostngContractDomain_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdHostngContractDomain.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
        private void grdHostngContractDomain_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "delete" :
                    //gSDMS_BL.ud_gSDMS_CONTRACT_Del(e.CommandArgument.ToString(), false);
                    break;
                case "detail" :
                    var Hosting_Contract_ID = Convert.ToInt32(e.CommandArgument.ToString());
                    WebUtils.SetupRedirect(this , SiteRoot + "/gsdmsmodule/contract/contract_hostdomain_edit.aspx?pageid=29&id=" + Hosting_Contract_ID);
                    break;
      
            }

        }
        void grdHostngContractDomain_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LoadGrid();
        }
        void grdHostngContractDomain_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;
            
            LoadGrid();
        }

        void grdHostngContractDomain_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (_siteUser == null) return;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //int createBy = (int)DataBinder.Eval(e.Row.DataItem , "CreatedBy");
      

                //if ((bool)DataBinder.Eval(e.Row.DataItem, "Hosting_Active")
                //    || (bool)DataBinder.Eval(e.Row.DataItem, "Domain_Active"))
                //{
                //    //e.Row.FindControl("btnEdit").Visible = true;
                //    e.Row.FindControl("btnDelete").Visible = true;
                //}
            }
        }

        void btnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            LoadGrid();


        }

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            grdHostngContractDomain.PageIndexChanging += grdHostngContractDomain_PageIndexChanging;
            grdHostngContractDomain.RowCommand += grdHostngContractDomain_RowCommand;
            grdHostngContractDomain.RowDeleting += grdHostngContractDomain_RowDeleting;
            grdHostngContractDomain.Sorting += grdHostngContractDomain_Sorting;
            grdHostngContractDomain.RowDataBound += grdHostngContractDomain_RowDataBound;
            btnSearch.Click += btnSearch_Click;

            //btnFinish.Click += btnFinish_Click;
            SuppressPageMenu();
        }

        

        void btnAddNew_Click(object sender, EventArgs e)
        {
            WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/contract/Contract_HostDomain_Edit.aspx?pageid=29");
        }

        

      

        
        

        void btnFinish_Click(object sender, EventArgs e)
        {
            WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}